<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport()">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="default" @click="handleImport()">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <!-- <Popconfirm placement="bottom" :title="t('是否手动推送？')" @confirm="handlets">
          <a-button type="success" ghost  v-auth="'wh:rd01:rd01:edit'">
            <Icon icon="ant-design:check-outlined" /> {{ t('手动推送') }}
          </a-button>
        </Popconfirm> -->
        <a-button type="error" @click="handleForm({ bred: '0' })" v-auth="'wh:rd01:rd01:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增红字') }}
        </a-button>
        <a-button type="primary" @click="handleForm({ bred: '1' })" v-auth="'wh:rd01:rd01:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增蓝字') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ code: record.code })">
          {{ record.code }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <BpmRuntimeTrace @register="registerTraceModal" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsWmsWhRd01Rd01List">
  import { unref, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { downloadByUrl } from '/@/utils/file/download';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { rd01Delete, rd01ListData,batchUpdateDate } from '/@/api/wms/wh/rd01/rd01';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { BpmRuntimeTrace } from '/@/components/Bpm';
  import { FormProps } from '/@/components/Form';
  import { companyTreeData } from '/@/api/sys/company';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';
  import { useUserStore } from '/@/store/modules/user';
  import { Popconfirm } from 'ant-design-vue';
  const userStore = useUserStore();

  const { t } = useI18n('wh.rd01.rd01');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'ant-design:book-outlined',
    value: meta.title || t('采购入库管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
     
      {
        label: t('单据日期'),
        field: 'ddate',
        component: 'RangePicker',
      },
      {
        label: t('入库单号'),
        field: 'code',
        component: 'Input',
      },
      {
        label: t('业务类型'),
        field: 'busType',
        component: 'Select',
        componentProps: {
          dictType: 'wh_rd01_bus_type',
          allowClear: true,
        },
      },
      {
        label: t('红蓝标志'),
        field: 'bred',
        component: 'Select',
        componentProps: {
          dictType: 'sys_red_blue',
          allowClear: true,
        },
      },

      {
        label: t('仓库'),
        field: 'basWare.cwhname',
        component: 'Input',
      },
      {
        label: t('制单时间'),
        field: 'createDate',
        component: 'RangePicker',
      },
      {
        label: t('制单人'),
        field: 'createByName',
        component: 'Input',
      },
      {
        label: t('备注'),
        field: 'remarks',
        component: 'Input',
      },
      // {
      //   label: t('数据来源'),
      //   field: 'sourceFlag',
      //   component: 'Select',
      //   componentProps: {
      //     dictType: 'bas_source_type',
      //     allowClear: true,
      //   },
      // },
       
      {
        label: t('审批状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'bpm_biz_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
    fieldMapToTime: [['ddate', ['ddate_gte', 'ddate_lte']],['createDate', ['createDate_gte', 'createDate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('入库单号'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 120,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('单据日期'),
      dataIndex: 'ddate',
      key: 'a.ddate',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('业务类型'),
      dataIndex: 'busType',
      key: 'a.bus_type',
      sorter: true,
      width: 100,
      align: 'left',
      dictType: 'wh_rd01_bus_type',
    },
    {
      title: t('红蓝标志'),
      dataIndex: 'bred',
      key: 'a.bred',
      sorter: true,
      width: 90,
      align: 'center',
      dictType: 'sys_red_blue',
    },
   
    {
      title: t('仓库'),
      dataIndex: 'basWare.cwhname',
      key: 'a.wh_code',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('制单时间'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('制单人'),
      dataIndex: 'createByName',
      key: 'a.create_by_name',
      sorter: true,
      width: 90,
      align: 'left',
    },
    // {
    //   title: t('修改人'),
    //   dataIndex: 'updateByName',
    //   key: 'a.update_by_name',
    //   sorter: true,
    //   width: 90,
    //   align: 'left',
    // },

    {
      title: t('备注信息'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
    //   {
    //   title: t('数据来源'),
    //   dataIndex: 'sourceFlag',
    //   key: 'a.source_flag',
    //   sorter: true,
    //   width: 90,
    //   align: 'center',
    //   dictType: 'bas_source_type',
    //   // fixed: 'right',
    // },
   
    {
      title: t('审批状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'bpm_biz_status',
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 90,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑采购入库'),
        onClick: handleForm.bind(this, { code: record.code }),
        auth: 'wh:rd01:rd01:edit',
        ifShow: () => record.status == '9',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除采购入库'),
        popConfirm: {
          title: t('是否确认删除采购入库'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'wh:rd01:rd01:edit',
        ifShow: () => record.status == '9',
      },
      // {
      //   icon: 'fluent:flowchart-20-regular',
      //   title: t('流程追踪'),
      //   onClick: handleTrace.bind(this, record),
      //   ifShow: () => record.status != '9',
      // },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows, setProps }] = useTable({
    api: rd01ListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/wh/rd01/rd01/exportData',
      params: getForm().getFieldsValue(),
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDelete(record: Recordable) {
    const params = { code: record.code };
    const res = await rd01Delete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }
  async function handlets() {
    if (getSelectRows().length) {
      let selIds =  getSelectRows().map((item) => item.code).join(',')
      const res = await batchUpdateDate({ selIds });
      showMessage(res.message);
      handleSuccess({});
    } else {
      showMessage('请先选择数据');
    }
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    traceModel(true, { formKey: 'flow_wh_rd01', bizKey: record.id });
  }



  onMounted(async () => {
    setProps()
    handleCompany();
  });

  async function handleCompany() {
    await getForm().setFieldsValue({
      // companyCode: userStore.getProjecte.code,
      status:''
    });
    reload();
    await getForm().setFieldsValue({
      // companyCode: userStore.getProjecte.code,
      status:''
    });
  }
</script>
